home *** CD-ROM | disk | FTP | other *** search
- Path: netaxs.com!not-for-mail
- From: grulm@netaxs.com (J. A. McNamara)
- Newsgroups: comp.lang.c
- Subject: Re: binary tree question
- Date: 23 Mar 1996 02:59:59 GMT
- Organization: Philadelphia's Complete Internet Provider
- Message-ID: <4ivpff$a6j@netaxs.com>
- References: <4isglj$cgg@netaxs.com> <4iumkjINNsp3@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: unix5.netaxs.com
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- Kazimir Kylheku (c2a192@ugrad.cs.ubc.ca) wrote:
- : In article <4isglj$cgg@netaxs.com>, J. A. McNamara <grulm@netaxs.com> wrote:
-
- : > /* blah blah blah I wanna free the tree */
- : >
- : >link *inorder(tree_n *tn, link *tail, int *i) /* read tree into list */
- : > {
- : > if (tn != NULL) {
- : > tail = inorder(tn->l, tail, i);
- : > tail = addlink(tail, tn->f); /* hand data ptr to list */
- : > (*i)++; /* keep track of total */
- : > tail = inorder(tn->r, tail, i);
- : > }
- : > if (tn!=NULL) free(tn); tn=NULL;
- : > return tail;
- : > }
-
- : This is not a pure ``inorder'' traversal. The nodes are being added to the
- : linked list in order, but you are freeing in post-order (children are visited
- : first, then you free the parent).
-
- hmmm, yeah, I see yr point. would that affect anything? I think I did it
- that way originally because I was freeing the children, so I wanted it in
- a safe place.
-
- : Why don't you combine the two if() statements into one? They have precisely the
- : same expression, and tn is never assigned to in the body of the first one, so
- : the evaluation of the expression doesn't change.
- : You do not need to assign NULL to the ``tn'' pointer after you have invoked
- : free() on it. This is a waste of time, since it has been passed to the
- : procedure by value. You are only affecting the value of the function parameter
- : ``tn'', not any external object. This value is never used, so the compiler will
- : throw that assignment away at the optimization phase.
-
- heh heh heh . . . I noticed these two issues while posting; I didn't
- change the code because I *knew* it compiled and worked this way. this is
- what happens when an inexperienced programmer writes > 400 lines of code
- in like four days. let this be a warning to all. the program actually
- does work, oddly enough.
-
- : >free(tn->l); tn->l=NULL;
- : >free(tn->r); tn->r=NULL;
- : >
- : >. . . which gave me some serious garbage, though I'm not quite sure why.
-
- : Because you probably did not check whether the children are null pointers or
- : not, only whether tn is a null pointer. Just because tn is a valid tree node
- : doesn't mean that tn->l or tn->r are.
-
- jesus, I'm really batting 1000 here. I don't grok why that would give the
- list pointers to garbage, though; that stuff was postorder, just like the
- free() in the whole function I posted. ??
-
- : The above modification will also fail to
- : free the root node.
-
- ah! small potatoes, but worth noting.
-
- : Your code appears fine. That it's not actually freeing memory is not
- : surprising. Many free() implementations don't free memory, they only return
- : free blocks to a list from where subsequent malloc() invocations can obtain
- : memory quickly.
-
- how isn't that freeing? if this'll clarify things any, I want to free the
- tree to make room for the list, so one shrinks as the other grows. I'd
- guess (!) that the malloc() in the addlink() function would then pull
- memory off that list, right? (and both structs are the same size, three
- pointers). does it then not free the memory for *non*-malloc() purposes?
- Or only sorta free it? This is something I'm kinda foggy about in
- general.
-
-
- --
- j a mcnamara aramancm a j
- grulm@netaxs.com moc.sxaten@mlurg
-